package com.zzx.es;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.http.HttpHost;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;

import java.io.IOException;

/**
 * @ClassName: com.zzx.es.ESTest_Client
 * @Description: ES文档查询测试类
 * @Author zhouzhixin
 * @Date 2021/8/13
 * @Version 1.0
 */
public class ESTest_Doc_Advanced_Query {
    public static void main(String[] args) {
        // 建立可自动关闭的连接
        try (RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(new HttpHost("127.0.0.1", 9200, "http"))
        )) {
            // 全量查询
//            SearchRequest request = new SearchRequest();
//            request.indices("user");
//
//            request.source(new SearchSourceBuilder().query(QueryBuilders.matchAllQuery()));
//            // 发送请求
//            SearchResponse response = client.search(request, RequestOptions.DEFAULT);
//            // 返回结果
//            SearchHits hits = response.getHits();
//            System.out.println(hits.getTotalHits());
//            System.out.println(response.getTook());
//            for (SearchHit hit : hits) {
//                System.out.println(hit.getSourceAsString());
//            }

            // 条件查询
//            SearchRequest request = new SearchRequest();
//            request.indices("user");
//
//            request.source(new SearchSourceBuilder().query(QueryBuilders.termQuery("sex", "2")));
//            // 发送请求
//            SearchResponse response = client.search(request, RequestOptions.DEFAULT);
//            // 返回结果
//            SearchHits hits = response.getHits();
//            System.out.println(hits.getTotalHits());
//            System.out.println(response.getTook());
//            for (SearchHit hit : hits) {
//                System.out.println(hit.getSourceAsString());
//            }

            // 分页查询
//            SearchRequest request = new SearchRequest();
//            request.indices("user");
//
//            SearchSourceBuilder sourceBuilder = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());
//            sourceBuilder.from(2);
//            sourceBuilder.size(2);
//            request.source(sourceBuilder);
//            // 发送请求
//            SearchResponse response = client.search(request, RequestOptions.DEFAULT);
//            // 返回结果
//            SearchHits hits = response.getHits();
//            System.out.println(hits.getTotalHits());
//            System.out.println(response.getTook());
//            for (SearchHit hit : hits) {
//                System.out.println(hit.getSourceAsString());
//            }

            // 查询排序
//            SearchRequest request = new SearchRequest();
//            request.indices("user");
//
//            SearchSourceBuilder sourceBuilder = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());
//            sourceBuilder.sort("age", SortOrder.ASC);
//            request.source(sourceBuilder);
//            // 发送请求
//            SearchResponse response = client.search(request, RequestOptions.DEFAULT);
//            // 返回结果
//            SearchHits hits = response.getHits();
//            System.out.println(hits.getTotalHits());
//            System.out.println(response.getTook());
//            for (SearchHit hit : hits) {
//                System.out.println(hit.getSourceAsString());
//            }

            // 字段过滤
//            SearchRequest request = new SearchRequest();
//            request.indices("user");
//
//            SearchSourceBuilder sourceBuilder = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());
//            // 排除字段
//            String[] excludes = {};
//            // 包含字段
//            String[] includes = {"age"};
//            sourceBuilder.fetchSource(includes, excludes);
//            request.source(sourceBuilder);
//            // 发送请求
//            SearchResponse response = client.search(request, RequestOptions.DEFAULT);
//            // 返回结果
//            SearchHits hits = response.getHits();
//            System.out.println(hits.getTotalHits());
//            System.out.println(response.getTook());
//            for (SearchHit hit : hits) {
//                System.out.println(hit.getSourceAsString());
//            }

            // 组合查询
//            SearchRequest request = new SearchRequest();
//            request.indices("user");
//
//            SearchSourceBuilder sourceBuilder = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());
//            BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
////            boolQuery.must(QueryBuilders.matchQuery("age", 18));
////            boolQuery.mustNot(QueryBuilders.matchQuery("sex", "1"));
//            boolQuery.should(QueryBuilders.matchQuery("age", 18));
//            boolQuery.should(QueryBuilders.matchQuery("age", 16));
//
//            sourceBuilder.query(boolQuery);
//            request.source(sourceBuilder);
//            // 发送请求
//            SearchResponse response = client.search(request, RequestOptions.DEFAULT);
//            // 返回结果
//            SearchHits hits = response.getHits();
//            System.out.println(hits.getTotalHits());
//            System.out.println(response.getTook());
//            for (SearchHit hit : hits) {
//                System.out.println(hit.getSourceAsString());
//            }

            // 范围查询
//            SearchRequest request = new SearchRequest();
//            request.indices("user");
//
//            SearchSourceBuilder sourceBuilder = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());
//            RangeQueryBuilder rangeQuery = QueryBuilders.rangeQuery("age");
//            rangeQuery.gte("18");
//
//            sourceBuilder.query(rangeQuery);
//            request.source(sourceBuilder);
//            // 发送请求
//            SearchResponse response = client.search(request, RequestOptions.DEFAULT);
//            // 返回结果
//            SearchHits hits = response.getHits();
//            System.out.println(hits.getTotalHits());
//            System.out.println(response.getTook());
//            for (SearchHit hit : hits) {
//                System.out.println(hit.getSourceAsString());
//            }

            // 模糊查询
//            SearchRequest request = new SearchRequest();
//            request.indices("user");
//
//            SearchSourceBuilder sourceBuilder = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());
//            FuzzyQueryBuilder fuzziness = QueryBuilders.fuzzyQuery("name", "z").fuzziness(Fuzziness.ONE);
//
//            sourceBuilder.query(fuzziness);
//            request.source(sourceBuilder);
//            // 发送请求
//            SearchResponse response = client.search(request, RequestOptions.DEFAULT);
//            // 返回结果
//            SearchHits hits = response.getHits();
//            System.out.println(hits.getTotalHits());
//            System.out.println(response.getTook());
//            for (SearchHit hit : hits) {
//                System.out.println(hit.getSourceAsString());
//            }

            // 高亮查询
//            SearchRequest request = new SearchRequest();
//            request.indices("user");
//
//            SearchSourceBuilder sourceBuilder = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());
//            TermQueryBuilder queryBuilder = QueryBuilders.termQuery("name", "zs");
//            HighlightBuilder highlightBuilder = new HighlightBuilder();
//            highlightBuilder.preTags("<font color='red'>");
//            highlightBuilder.postTags("</font>");
//            highlightBuilder.field("name");
//
//            sourceBuilder.highlighter(highlightBuilder);
//            sourceBuilder.query(queryBuilder);
//            request.source(sourceBuilder);
//            // 发送请求
//            SearchResponse response = client.search(request, RequestOptions.DEFAULT);
//            // 返回结果
//            SearchHits hits = response.getHits();
//            System.out.println(hits.getTotalHits());
//            System.out.println(response.getTook());
//            for (SearchHit hit : hits) {
//                System.out.println(hit.getSourceAsString());
//            }

            // 聚合查询
//            SearchRequest request = new SearchRequest();
//            request.indices("user");
//
//            SearchSourceBuilder sourceBuilder = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());
//            MaxAggregationBuilder aggregationBuilder = AggregationBuilders.max("maxAge").field("age");
//
//            sourceBuilder.aggregation(aggregationBuilder);
//            request.source(sourceBuilder);
//            // 发送请求
//            SearchResponse response = client.search(request, RequestOptions.DEFAULT);
//            // 返回结果
//            SearchHits hits = response.getHits();
//            System.out.println(hits.getTotalHits());
//            System.out.println(response.getTook());
//            System.out.println("response:" + new ObjectMapper().writeValueAsString(response.getAggregations()));
//            for (SearchHit hit : hits) {
//                System.out.println(hit.getSourceAsString());
//            }

            // 分组查询
            SearchRequest request = new SearchRequest();
            request.indices("user");

            SearchSourceBuilder sourceBuilder = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());
            TermsAggregationBuilder aggregationBuilder = AggregationBuilders.terms("ageGroup").field("age");

            sourceBuilder.aggregation(aggregationBuilder);
            request.source(sourceBuilder);
            // 发送请求
            SearchResponse response = client.search(request, RequestOptions.DEFAULT);
            // 返回结果
            SearchHits hits = response.getHits();
            System.out.println(hits.getTotalHits());
            System.out.println(response.getTook());
            System.out.println("response:" + new ObjectMapper().writeValueAsString(response.getAggregations()));
            for (SearchHit hit : hits) {
                System.out.println(hit.getSourceAsString());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
